package com.hmall.order.service.impl;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.client.UserClient;
import com.hmall.client.itemClient;
import com.hmall.common.dto.ResultDTO;
import com.hmall.common.pojo.Address;
import com.hmall.common.pojo.Item;
import com.hmall.common.pojo.User;
import com.hmall.order.mapper.OrderDetailMapper;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.*;
import com.hmall.order.service.OrderService;

import com.hmall.order.utils.ThreadLocalUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

/**
 * @Description:
 * @Author: 315QK
 * @Date: 2021/12/21/10:52
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper,Order> implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderDetailMapper rderDetailiapper;

    @Autowired
    private itemClient itemClient;

    @Autowired
    private UserClient userClient;

    @Override
    public Long saveNewOrder(ViewSource viewSource) {

        //商品微服务提供FeignClient，实现根据id查询商品的接口
        Item item = itemClient.getById(viewSource.getItemId());
        //基于商品价格、购买数量计算商品总价：totalFee
        Long totalFee=item.getPrice()*viewSource.getNum();
        //封装Order对象，初始status为未支付
        Order order = new Order();
        order.setTotalFee(totalFee);
        order.setPaymentType(viewSource.getPaymentType());
        order.setUserId(ThreadLocalUtil.getUserId());
        order.setStatus(1);
        order.setUserId(ThreadLocalUtil.getUserId());
        //将Order写入数据库tb_order表中
        orderMapper.insert(order);
        //将商品信息、orderId信息封装为OrderDetail对象，写入tb_order_detail表
        Long orderId = order.getId();
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setOrderId(orderId);
        orderDetail.setItemId(item.getId());
        orderDetail.setNum(viewSource.getNum());
        orderDetail.setName(item.getName());
        orderDetail.setPrice(item.getPrice());
        orderDetail.setSpec(item.getSpec());
        orderDetail.setImage(item.getImage());
        System.out.println(orderDetail);
        rderDetailiapper.insert(orderDetail);

        //根据addressId查询user-service服务，获取地址信息
        Long userId = ThreadLocalUtil.getUserId();
        Address address = userClient.getAddress(viewSource.getAddressId(),userId);

        //将地址封装为OrderLogistics对象，写入tb_order_logistics表
        OrderLogistics orderLogistics = new OrderLogistics();
        BeanUtils.copyProperties(address,orderLogistics);

        //在item-service提供减库存接口，并编写FeignClient
        Long id = item.getId();
        itemClient.reduceOne(id);

        return orderId;
    }
    @Scheduled(cron = "0 */1 * * * ?") // 每隔1分钟
    public void test(){
        int status=1;

        orderMapper.updateOrders(status, LocalDateTime.now());
    }
}
